//表单验证
$(function(){
	var times = 60;
    function sendVerifyCode(){
        if(times == 0){
            $('.yanzhengma').text('发送验证码('+times+'s)');
            $('.yanzhengma').prop('disabled',false);
            $('.yanzhengma').text('发送验证码');
            $.cookie("yanzhengma", null);
            times = 60;
            return;
        }
        $('.yanzhengma').text('发送验证码('+times+'s)');
        times--;
        setTimeout(sendVerifyCode,1000);
    }
    
    //如果cookie里存在true，则说明发送过，则置为disable
	var verifyCodeDisable = $.cookie("yanzhengma");
	if(verifyCodeDisable == "true"){
		$('.yanzhengma').prop('disabled',true);
		sendVerifyCode();
	}else{
		$('.yanzhengma').prop('disabled',false);
	}
    
    $('.yanzhengma').on('click',function(){
    	var loginName = $('#loginName').val();
		if(loginName == null || loginName == ""){
			layer.msg("手机号不能为空!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(loginName)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		$.ajax({
			url:baselocation+'/sendVerifyCode/'+loginName,
			type:'get',
			dataType:'json',
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					layer.msg(result.message,{icon:1,time:4000});
					$.cookie("yanzhengma", "true", { path: "/", expires: 10000});
					$('.yanzhengma').prop('disabled',true);
			        sendVerifyCode();
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
    });
	$('#verifyCode').blur(function () {
		var loginName = $('#loginName').val();
		if(loginName == null || loginName == ""){
			layer.msg("手机号不能为空!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(loginName)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		var verifyCode = $('#verifyCode').val();
		if(verifyCode == null || verifyCode == ''){
			layer.msg("验证码不能为空!",{icon:2,time:2000});
			return;
		}
		
		$.ajax({
			url:baselocation+'/checkVerifyCode/'+loginName+'/'+verifyCode,
			type:'get',
			dataType:'json',
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	$('#registerBtn').click(function () {
		//获取验证器
		var loginName = $('#loginName').val();
		var password = $('#password').val();
		var repassword = $('#repassword').val();
		var verifyCode = $('#verifyCode').val();
		
		if(loginName == null || loginName == ""){
			layer.msg("手机号不能为空!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(loginName)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		
		if(verifyCode == null || verifyCode == ""){
			layer.msg("验证码不能为空!",{icon:2,time:2000});
			return;
		}
		if(password == null || password == ""){
			layer.msg("密码不能为空!",{icon:2,time:2000});
			return;
		}
		if(!isCode(password)){
			layer.msg("亲,密码只能是英文字母和数字组合,不区分大小写哦!",{icon:2,time:2000});
			return;
		}
		if(password.length < 6){
			layer.msg("请输入至少6位密码!",{icon:2,time:2000});
			return;
		}
		if(repassword == null || repassword == ""){
			layer.msg("重复密码不能为空!",{icon:2,time:2000});
			return;
		}
		
		if(repassword != password){
			layer.msg("两次密码输入不一致!",{icon:2,time:2000});
			return;
		}
		
		var user = {};
		user["loginName"] = loginName;
		user["pwd"] = password;
		user["repwd"] = repassword;
		user["email"] = verifyCode;
		$('#registerBtn').attr("disabled",true);
		$.ajax({
			url:baselocation+'/register',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(user),
			success:function(result){
				$('#registerBtn').attr("disable",false);
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					$.cookie("userId", result.data.id, { path: "/", expires: 100000});
					$.cookie("userToken", result.data.pwd, { path: "/", expires: 100000});
					window.location.href=baselocation+'/done-user';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	$('#tiyan').click(function () {
		var user = {};
		user["loginName"] = "13331071314";
		user["pwd"] = "111111";
		$.ajax({
			url:baselocation+'/login',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(user),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					$.cookie("userId", result.data.id, { path: "/", expires: 100000 });
					$.cookie("userToken", result.data.pwd, { path: "/", expires: 100000});
					window.location.href=baselocation+'/index';
				}
			}
		});
	})
	
	$('#loginBtn').click(function () {
		//获取验证器
		var loginName = $('#loginName').val();
		var password = $('#password').val();
		if(loginName == null || loginName == ""){
			layer.msg("手机号不能为空!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(loginName)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		if(password == null || password == ""){
			layer.msg("密码不能为空!",{icon:2,time:2000});
			return;
		}
		if(password.length < 6){
			layer.msg("请输入至少6位密码!",{icon:2,time:2000});
			return;
		}
		var user = {};
		user["loginName"] = loginName;
		user["pwd"] = password;
		$('#loginBtn').attr("disabled",true);
		$.ajax({
			url:baselocation+'/login',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(user),
			success:function(result){
				$('#loginBtn').attr("disabled",false);
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					$.cookie("userId", result.data.id, { path: "/", expires: 100000 });
					$.cookie("userToken", result.data.pwd, { path: "/", expires: 100000});
					window.location.href=baselocation+'/index';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	
	$('#doneUserBtn').click(function () {
		//获取验证器
		var name = $('#name').val();
		var weixin = $('#weixin').val();
		var weixinQrCodeImage = $('#weixinQrCodeImage').attr("src");
		var zhifubao = $('#zhifubao').val();
		var zhifubaoQrCodeImage = $('#zhifubaoQrCodeImage').attr("src");
		
		var email = $('#email').val();
		
		var qq = $('#qq').val();
		var sex = $('#sex').val();
		var idCard = $('#idCard').val();
		var address = $('#address').val();
		
		if(name == null || name == ""){
			layer.msg("请输入您的姓名或昵称!",{icon:2,time:2000});
			return;
		}
		if(name.length > 32){
			layer.msg("姓名不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixin == null || weixin == ""){
			layer.msg("请输入微信号以便客户可以及时联系您!",{icon:2,time:2000});
			return;
		}
		if(weixin.length > 32){
			layer.msg("微信号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage == null || weixinQrCodeImage == ""){
			layer.msg("请上传微信二维码,客户可以更方便联系您!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage.length > 512){
			layer.msg("微信二维码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(zhifubao == null || zhifubao == ""){
			layer.msg("请输入支付宝账号以便客户可以及时联系您!",{icon:2,time:2000});
			return;
		}
		if(zhifubao.length > 32){
			layer.msg("支付宝账号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(zhifubaoQrCodeImage == null || zhifubaoQrCodeImage == ""){
			layer.msg("请上传支付宝二维码,客户可以更方便联系您!",{icon:2,time:2000});
			return;
		}
		if(zhifubaoQrCodeImage.length > 512){
			layer.msg("支付宝二维码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(email != null && email != ""){
			email = email.trim();
			if(!isEmail(email)){
				layer.msg("邮箱不符合规范!",{icon:2,time:2000});
				return;
			}
		}
		if(email != null && email.length > 32){
			layer.msg("邮箱不能超过32位!",{icon:2,time:2000});
			return;
		}
		
		if(qq != null && qq.length > 12){
			layer.msg("qq号不能超过12位!",{icon:2,time:2000});
			return;
		}
		
		if(idCard != null && idCard.length > 20){
			layer.msg("身份证号不能超过20位!",{icon:2,time:2000});
			return;
		}
		
		if(address != null && address.length > 512){
			layer.msg("地址不能超过512位!",{icon:2,time:2000});
			return;
		}
		var user = {};
		user["name"] = name;
		user["weixin"] = weixin;
		user["weixinQrCode"] = weixinQrCodeImage;
		user["zhifubao"] = zhifubao;
		user["zhifubaoQrCode"] = zhifubaoQrCodeImage;
		user["email"] = email;
		user["qq"] = qq;
		user["sex"] = sex;
		user["idCard"] = idCard;
		user["address"] = address;
		$('#doneUserBtn').attr("disabled",true);
		$.ajax({
			url:baselocation+'/done-user',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(user),
			success:function(result){
				$('#doneUserBtn').attr("disabled",false);
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/done-shop';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	
	$('#doneShopBtn').click(function () {
		//获取验证器
		var name = $('#name').val();
		var code = $('#code').val();
		var weixinQrCodeImage = $('#weixinQrCodeImage').attr("src");
		var zhifubaoQrCodeImage = $('#zhifubaoQrCodeImage').attr("src");
		var logoImage = $('#logoImage').attr("src");
		var description = $('#description').val();
		if(code == null || code == ""){
			layer.msg("请输入店铺的助记码,以方便您店铺的推广!",{icon:2,time:2000});
			return;
		}
		if(!isCode(code)){
			layer.msg("亲,助记码只能是英文字母和数字组合,不区分大小写哦!",{icon:2,time:2000});
			return;
		}
		if(code.length > 32){
			layer.msg("店铺助记码不能超过32位,越短越好哦!",{icon:2,time:2000});
			return;
		}
		if(name == null || name == ""){
			layer.msg("请为您的店铺起个响亮的名字,以便客户能快速记住!",{icon:2,time:2000});
			return;
		}
		if(name.length > 32){
			layer.msg("姓名不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage == null || weixinQrCodeImage == ""){
			layer.msg("请上传微信收钱码,方便客户向您付款!",{icon:2,time:2000});
			return;
		}
		if(weixinQrCodeImage.length > 512){
			layer.msg("微信收钱码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		
		if(zhifubaoQrCodeImage == null || zhifubaoQrCodeImage == ""){
			layer.msg("请上传支付宝收钱码,方便客户向您付款!",{icon:2,time:2000});
			return;
		}
		if(zhifubaoQrCodeImage.length > 512){
			layer.msg("支付宝收钱码url不能超过512位!",{icon:2,time:2000});
			return;
		}
		if(logoImage == null || logoImage == ""){
			layer.msg("请上传您店铺的logo!",{icon:2,time:2000});
			return;
		}
		if(description != null && description.length > 512){
			layer.msg("店铺描述不能超过512位!",{icon:2,time:2000});
			return;
		}
		var shop = {};
		shop["code"] = code;
		shop["name"] = name;
		shop["weixinQrCode"] = weixinQrCodeImage;
		shop["zhifubaoQrCode"] = zhifubaoQrCodeImage;
		shop["logo"] = logoImage;
		$('#doneShopBtn').attr("disabled",true);
		$.ajax({
			url:baselocation+'/done-shop',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(shop),
			success:function(result){
				$('#doneShopBtn').attr("disabled",false);
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/index';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	$('#forgetPwdBtn').click(function () {
		alert("请联系管理员！电话：13331071314");
	})
	
});